//
//  JTNavigationController.m
//  百思不得姐
//
//  Created by sun lei on 16/11/19.
//  Copyright © 2016年 BeiJingWangDianJieTong. All rights reserved.
//

#import "JTNavigationController.h"

@interface JTNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation JTNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    /* 
     代理清空也可以实现这个效果。
     原因：这个代理仅仅是告诉这个手势什么时候好用什么时候不好用；如果没有这个代理，也就是说没有人去控制这个代理是不是好用，而它默认是好用的
     Bug：默认好用也会出现UINavigationController的根视图控制器出现左滑后无法点击的情况，所以不推荐使用
     */
//    [self.interactivePopGestureRecognizer setDelegate:nil];
    [self.interactivePopGestureRecognizer setDelegate:self];
    
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
}

/**
 重写push方法的目的:拦截所有push进来的子控制器

 @param viewController 刚刚push进来的子控制器
 @param animated 是否带动画
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (self.childViewControllers.count) { // 如果viewController不是最早push进来的子控制器
        
        // 左上角
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [backButton setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [backButton setTitle:@"返回" forState:UIControlStateNormal];
        [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [backButton sizeToFit];
        // 这句代码放在sizeToFit后面
        [backButton setContentEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)];
        [backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
        [viewController.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:backButton]];
        
        // 隐藏底部的工具条
        [viewController setHidesBottomBarWhenPushed:YES];
    }
    
    // 所有设置搞定后，再push控制器
    [super pushViewController:viewController animated:animated];
}

/*
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    
    return [super popViewControllerAnimated:NO];
}

- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    return [super popToViewController:viewController animated:NO];
}

- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
    
    return [super popToRootViewControllerAnimated:NO];
}
 */

- (void)back:(UIButton *)sender {
    
    [self popViewControllerAnimated:YES];
}

#pragma mark - <UIGestureRecognizerDelegate>
/**
 手势识别器对象会调用这个代理方法来决定手势是否有效

 @param gestureRecognizer 手势
 @return YES:手势有效 NO:手势无效
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    // 手势何时有效:当导航控制器的子控制器个数 > 1时就有效
    // 原因：导航控制器的根视图左滑的时候，依然pop了视图控制器，就会导致在导航控制器又push之后程序无法继续操作的Bug
    return self.childViewControllers.count > 1;
}

@end
